# 2006... Coaliciones:
data$"2006"$PRI <- data$"2006"$ALIANZA.POR.MÉXICO
data$"2006"$PRD <- data$"2006"$POR.EL.BIEN.DE.TODOS
# 2009... Coalicion parcial PRI-PVEM
data$"2009"$PRI <- rowSums(data$"2009"[,c("PRI","PRIMERO.MÉXICO")], na.rm=TRUE)
# 2012... Coaliciones parciales/totales:
data$"2012"$PRI <- rowSums(data$"2012"[,c("PRI","COALICIÓN.PRI.PVEM")], na.rm=TRUE)
data$"2012"$PRD <- rowSums(data$"2012"[,c("PRD","COALICIÓN.PRD.PT.MC","COALICIÓN.PRD.PT","COALICIÓN.PRD.MC")], na.rm=TRUE)
# Partidos relevantes, total, lista nominal
data <- lapply(data, function(x) x[,c("PAN","PRI","PRD")])
#------------------------------
# Wasted, surplus and effective
#------------------------------
parties <- c("PAN","PRI","PRD")
# Calcular primero y segundo lugar por distrito
first.second <- function(x) {
x$First <- apply(x[,parties], 1, max)
x$Second <- apply(x[,parties], 1, function(i) sort(i)[2])
return(x)
}
data <- lapply(data, first.second)
# Surplus
surplus <- lapply(data, function(x) x[,parties]-x[,"Second"])
# Recodifica a 0 valores negativos
surplus <- lapply(surplus, function(x) {x[x<=0] <- 0; x})
data
replace.max <- function(x){x[which.max(x)] <- 0;x}
wasted <- lapply(data[parties], apply, 1, replace.max)
wasted <- lapply(data[,parties], apply, 1, replace.max)
wasted <- lapply(data, function(x) apply(x[,parties], 1, replace.max)
)
watsed
wasted
replace.max <- function(x) {x[which.max(x)] <- 0}
wasted <- lapply(data, function(x) apply(x[,parties], 1, replace.max)
)
wasted
replace.max <- function(x) {x[which.max(x)] <- 0; x}
wasted <- lapply(data, function(x) apply(x[,parties], 1, replace.max))
wasted
replace.max <- function(x) {x[which.max(x)] <- 0; t(x)}
wasted <- lapply(data, function(x) apply(x[,parties], 1, replace.max))
wasted
replace.max <- function(x) {x[which.max(x)] <- 0; x}
wasted <- lapply(data, function(x) apply(x[,parties], 1, replace.max))
wasted
?rowSums
replace.max <- function(x) {x[max(x)] <- 0; x}
wasted <- lapply(data, function(x) apply(x[,parties], 1, replace.max))
wasted
?max
wasted <- lapply(data, function(x) apply(x[,parties], 1, function(x) {x[which.max(x)] <- 0; x}))
wasted
foo <- data$"2012"
foo
foo[,candidates]==foo$First
foo[,parties]==foo$First
foo[,parties]!=foo$First
wasted <- lapply(data, function(x) (x[,parties]==x$First)*x[,parties])
wasted
wasted <- lapply(data, function(x) (x[,parties]!=x$First)*x[,parties])
wasted
surplus
lapply(data, function(x) x[,parties])
effective <- lapply(data, function(x) x[,parties]) -
lapply(surplus, function(x) x)
lapply(surplus, function(x) x)
effective <- lapply(data, function(x) (x[,parties]==x$First)*x[,parties])
effective
11941-5198
effective <- lapply(data, function(x) (x[,parties]==x$First)*x[,parties])
lapply(effective,x)-lapply(surplus,x)
lapply(effective, print)-lapply(surplus, print)
effective-surplus
foo2 <- data$"2009"
foo-foo2
lapply(names(data), print)
sapply(names(data), print)
sapply(names(data))
effective$"2012"
lapply(names(data), function(x) effective$x-surplus$x)
lapply(names(data), function(x) effective$x)
sapply(names(data), function(x) effective$x)
lapply(names(data), function(x) effective$x; x)
lapply(names(data), function(x) effective$x - surplus$x; data)
lapply(names(data), function(x) { effective$x-surplus$x; data} )
effective <- lapply(data, function(x) (x[,parties]==x$First)*x[,parties])
effective2 <- lapply(names(data), function(x) effective$x - surplus$x )
effective2
effective$"2012"- surplus$"2012"
tmp <- lapply(data, function(x) (x[,parties]==x$First)*x[,parties])
tmp$"2012"- surplus$"2012"
effective <- lapply(names(data), function(x) tmp$x - surplus$x)
effective
tmp <- lapply(data, function(x) (x[,parties]==x$First)*x[,parties])
# ... menos surplus
effective <- lapply(names(data), function(x) {tmp <- tmp$x - surplus$x; tmp})
effective
lapply(names(data), function(x) surplus$x)
lapply(names(data), function(x) {y<- surplus$x; y})
names(data)
parties
lapply(names(data), function(x) {y<- surplus$x; print(y)})
lapply(names(data), function(x) {y<- surplus$x; return(y)})
wasted
sapply(wasted, colSums)
wasted$"1994"
sapply(wasted, colSums, rm.na=TRUE)
sapply(wasted, colSums, na.rm=TRUE)
sapply(surplus, colSums, na.rm=TRUE)
sapply(surplus, colSums, na.rm=TRUE)
sapply(wasted, colSums, na.rm=TRUE)
sapply(data[,parties], colSums, na.rm=TRUE)
sapply(data, colSums, na.rm=TRUE)
foo <- sapply(wasted,  colSums, na.rm=TRUE)/sapply(data, colSums, na.rm=TRUE)
foo <- sapply(wasted,  colSums, na.rm=TRUE)/sapply(data, colSums, na.rm=TRUE)[1:3,]
foo
# Ganador menos segundo lugar
tmp <- lapply(data, function(x) x[,parties]-x[,"Second"])
# Recodifica a 0 valores negativos
surplus <- lapply(tmp, function(x) {x[x<=0] <- 0; x})
surplus
wasted <- lapply(data, function(x) (x[,parties]!=x$First)*x[,parties])
wasted
tmp <- lapply(data, function(x) (x[,parties]==x$First)*x[,parties])
tmp
myfunc <- function(year) {
temp <- (x$year[,parties]==x$year$First)*x$year[,parties]
return(temp)
}
myfunc("2012")
myfunc <- function(year) {
temp <- (data$year[,parties]==data$year$First)*data$year[,parties]
return(temp)
}
myfunc("2012")
year <- "2012"
data$year[,parties]
myfunc <- function(year) {
temp <- (data$year[,parties]==data$year$First)*data$year[,parties]
return(temp)
}
myfunc(2012)
data$"2012"[,partes]
data$"2012"[,parties]
data$"2012"[,parties]
data$2012[,parties]
paste("hola")
data$paste("2012")[,parties]
data
data[[1]]
data[1]
data[1][,parties]
data[[1]][,parties]
effective <- lapply(names(data), function(x) tmp[[x]] - surplus[[x]])
effective
rm(list = ls(all = TRUE)) #limpiar workspace
# ---------
# Read data
# ---------
setwd("~/Dropbox/DATA/DIPUTADOS_DISTRITO")
data <- lapply(list.files(), read.table, header=TRUE, sep="\t")
names(data) <- seq(1991, 2012, 3)
sapply(data, nrow)
lapply(data, names)
# ----------
# Clean data
# ----------
# 1991...ok
# 1994...ok
# 1997...PT gana un distrito
# 2000...Coaliciones:
data$"2000"$PAN <- data$"2000"$AC
data$"2000"$PRD <- data$"2000"$AM
# 2003... Coalición parcial PRI-PVEM
data$"2003"$PRI <- rowSums(data$"2003"[,c("PRI","APT")], na.rm=TRUE)
# 2006... Coaliciones:
data$"2006"$PRI <- data$"2006"$ALIANZA.POR.MÉXICO
data$"2006"$PRD <- data$"2006"$POR.EL.BIEN.DE.TODOS
# 2009... Coalicion parcial PRI-PVEM
data$"2009"$PRI <- rowSums(data$"2009"[,c("PRI","PRIMERO.MÉXICO")], na.rm=TRUE)
# 2012... Coaliciones parciales/totales:
data$"2012"$PRI <- rowSums(data$"2012"[,c("PRI","COALICIÓN.PRI.PVEM")], na.rm=TRUE)
data$"2012"$PRD <- rowSums(data$"2012"[,c("PRD","COALICIÓN.PRD.PT.MC","COALICIÓN.PRD.PT","COALICIÓN.PRD.MC")], na.rm=TRUE)
# Partidos relevantes, total, lista nominal
data <- lapply(data, function(x) x[,c("PAN","PRI","PRD")])
#------------------------------
# Wasted, surplus and effective
#------------------------------
parties <- c("PAN","PRI","PRD")
# Calcular primero y segundo lugar por distrito
first.second <- function(x) {
x$First <- apply(x[,parties], 1, max)
x$Second <- apply(x[,parties], 1, function(i) sort(i)[2])
return(x)
}
data <- lapply(data, first.second)
# Surplus
# Ganador menos segundo lugar
tmp <- lapply(data, function(x) x[,parties]-x[,"Second"])
# Recodifica a 0 valores negativos
surplus <- lapply(tmp, function(x) {x[x<=0] <- 0; x})
# Wasted votes
wasted <- lapply(data, function(x) (x[,parties]!=x$First)*x[,parties])
# Effective votes
# votos del primer lugar...
tmp <- lapply(data, function(x) (x[,parties]==x$First)*x[,parties])
# ... menos surplus
effective <- lapply(names(data), function(x) tmp[[x]] - surplus[[x]])
sapply(data, colSums, na.rm=TRUE)
sapply(surplus, colSums, na.rm=TRUE)
sapply(wasted,  colSums, na.rm=TRUE)
sapply(effective,  colSums, na.rm=TRUE)
sapply(data, colSums, na.rm=TRUE)[parties,]
sapply(data, colSums, na.rm=TRUE)[parties,]
sapply(surplus, colSums, na.rm=TRUE)
sapply(wasted,  colSums, na.rm=TRUE)
sapply(effective,  colSums, na.rm=TRUE)
sapply(data, colSums, na.rm=TRUE)[parties,]
sapply(surplus, colSums, na.rm=TRUE)
sapply(wasted,  colSums, na.rm=TRUE)
sapply(effective,  colSums, na.rm=TRUE)
5311005+229338
5311005+229338+50048
surplus <- sapply(surplus, colSums, na.rm=TRUE)
wasted <- sapply(wasted,  colSums, na.rm=TRUE)
effective <- sapply(effective,  colSums, na.rm=TRUE)
surplus+wasted+effective
sapply(data, colSums, na.rm=TRUE)[parties,]
rm(list = ls(all = TRUE)) #limpiar workspace
# ---------
# Read data
# ---------
setwd("~/Dropbox/DATA/DIPUTADOS_DISTRITO")
data <- lapply(list.files(), read.table, header=TRUE, sep="\t")
names(data) <- seq(1991, 2012, 3)
sapply(data, nrow)
lapply(data, names)
# ----------
# Clean data
# ----------
# 1991...ok
# 1994...ok
# 1997...PT gana un distrito
# 2000...Coaliciones:
data$"2000"$PAN <- data$"2000"$AC
data$"2000"$PRD <- data$"2000"$AM
# 2003... Coalición parcial PRI-PVEM
data$"2003"$PRI <- rowSums(data$"2003"[,c("PRI","APT")], na.rm=TRUE)
# 2006... Coaliciones:
data$"2006"$PRI <- data$"2006"$ALIANZA.POR.MÉXICO
data$"2006"$PRD <- data$"2006"$POR.EL.BIEN.DE.TODOS
# 2009... Coalicion parcial PRI-PVEM
data$"2009"$PRI <- rowSums(data$"2009"[,c("PRI","PRIMERO.MÉXICO")], na.rm=TRUE)
# 2012... Coaliciones parciales/totales:
data$"2012"$PRI <- rowSums(data$"2012"[,c("PRI","COALICIÓN.PRI.PVEM")], na.rm=TRUE)
data$"2012"$PRD <- rowSums(data$"2012"[,c("PRD","COALICIÓN.PRD.PT.MC","COALICIÓN.PRD.PT","COALICIÓN.PRD.MC")], na.rm=TRUE)
# Partidos relevantes, total, lista nominal
data <- lapply(data, function(x) x[,c("PAN","PRI","PRD")])
#------------------------------
# Wasted, surplus and effective
#------------------------------
parties <- c("PAN","PRI","PRD")
# Calcular primero y segundo lugar por distrito
first.second <- function(x) {
x$First <- apply(x[,parties], 1, max)
x$Second <- apply(x[,parties], 1, function(i) sort(i)[2])
return(x)
}
data <- lapply(data, first.second)
# Surplus
# Ganador menos segundo lugar
tmp <- lapply(data, function(x) x[,parties]-x[,"Second"])
# Recodifica a 0 valores negativos
surplus <- lapply(tmp, function(x) {x[x<=0] <- 0; x})
# Wasted votes
wasted <- lapply(data, function(x) (x[,parties]!=x$First)*x[,parties])
# Effective votes
# votos del primer lugar...
tmp <- lapply(data, function(x) (x[,parties]==x$First)*x[,parties])
# ... menos surplus
effective <- lapply(names(data), function(x) tmp[[x]] - surplus[[x]])
# Summaries
total <- sapply(data, colSums, na.rm=TRUE)[parties,]
surplus <- sapply(surplus, colSums, na.rm=TRUE)
wasted <- sapply(wasted,  colSums, na.rm=TRUE)
effective <- sapply(effective,  colSums, na.rm=TRUE)
surplus
surplus/total
plot(surplus/total)
?dotplot
dotchart(surplus/total)
dotchart(surplus/total, main="Voto Excedente")
dotchart(surplus/total, main="% Voto Excedente")
ncol(surplus/total)
plot(names(data), surplus/total["PAN",])
surplus/total["PAN",]
plot(names(data), surplus/total[1,])
surplus/total
surplus/total[1,]
surplus[1,]
(surplus/total)[1,]
plot(names(data), (surplus/total)[1,], type="l")
plot(names(data), (surplus/total)[2,], type="l")
plot(names(data), (surplus/total)[3,], type="l")
x <- surplus
plot(names(data), (x/total)[1,],
type="l",
xlab="",
ylab="Porcentaje",
main=main,
xaxt="n",
ylim=c(0,.7)
)
plot(names(data), (x/total)[1,],
type="l",
xlab="",
ylab="Porcentaje",
main="Main",
xaxt="n",
ylim=c(0,.7)
)
axis(1, at=names(data))
plot(names(data), (x/total)[1,],
type="l",
col="blue"
xlab="",
ylab="Porcentaje",
main="Main",
xaxt="n",
ylim=c(0,.7)
)
axis(1, at=names(data))
plot(names(data), (x/total)[1,],
type="l",
col="blue",
xlab="",
ylab="Porcentaje",
main="Main",
xaxt="n",
ylim=c(0,.7)
)
axis(1, at=names(data))
lines(names(data), (x/total)[2,], col="red")
lines(names(data), (x/total)[3,], col="yellow")
apply(x/total, 2, mx)
apply(x/total, 2, max)
plot(names(data), apply(x/total, 2, max),
type="n",
xlab="",
ylab="Porcentaje",
main="Main",
xaxt="n")
lines(names(data), (x/total)[1,], col="blue")
lines(names(data), (x/total)[2,], col="red")
lines(names(data), (x/total)[3,], col="yellow")
axis(1, at=names(data))
range(x/total)
plot(names(data), apply(x/total, 2, max),
type="n",
xlab="",
ylab="Porcentaje",
main="Main",
xaxt="n",
ylim=range(x/total))
plot(names(data), (x/total)[1,]),
type="n",
xlab="",
ylab="Porcentaje",
main="Main",
xaxt="n",
ylim=range(x/total))
lines(names(data), (x/total)[1,], col="blue")
lines(names(data), (x/total)[2,], col="red")
lines(names(data), (x/total)[3,], col="yellow")
axis(1, at=names(data))
(x/total)[1,]
plot(names(data), (x/total)[1,],
type="n",
xlab="",
ylab="Porcentaje",
main="Main",
xaxt="n",
ylim=range(x/total))
lines(names(data), (x/total)[1,], col="blue")
lines(names(data), (x/total)[2,], col="red")
lines(names(data), (x/total)[3,], col="yellow")
axis(1, at=names(data))
plot(names(data), (x/total)[1,]),
plot(names(data), (x/total)[1,]),
type="n",
xlab="",
ylab="Porcentaje",
main="Main",
xaxt="n",
ylim=range(x/total))
plot(names(data), (x/total)[1,],
type="n",
xlab="",
ylab="Porcentaje",
main="Main",
xaxt="n",
ylim=range(x/total))
lines(names(data), (x/total)[1,], col="blue")
lines(names(data), (x/total)[2,], col="red")
lines(names(data), (x/total)[3,], col="yellow")
axis(1, at=names(data))
plot(names(data), (x/total)[1,], type="n", xlab="", ylab="Porcentaje",
main="Main", xaxt="n", ylim=range(x/total))
lines(names(data), (x/total)[1,], col="blue")
lines(names(data), (x/total)[2,], col="red")
lines(names(data), (x/total)[3,], col="yellow")
axis(1, at=names(data))
my.plot <- function(x, main,) {
plot(names(data), (x/total)[1,], type="n", xlab="", ylab="Porcentaje",
main=main, xaxt="n", ylim=range(x/total))
lines(names(data), (x/total)[1,], col="blue")
lines(names(data), (x/total)[2,], col="red")
lines(names(data), (x/total)[3,], col="yellow")
axis(1, at=names(data))
}
# Plot
my.plot(surplus, "Votos excedentes")
my.plot <- function(x, main) {
plot(names(data), (x/total)[1,], type="n", xlab="", ylab="Porcentaje",
main=main, xaxt="n", ylim=range(x/total))
lines(names(data), (x/total)[1,], col="blue")
lines(names(data), (x/total)[2,], col="red")
lines(names(data), (x/total)[3,], col="yellow")
axis(1, at=names(data))
}
# Plot
my.plot(surplus, main="Votos excedentes")
my.plot(wasted, main="Votos desperdiciados")
my.plot(effective, main="Votos efectivos")
my.plot <- function(x, main) {
plot(names(data), x[1,], type="n", xlab="", ylab="Porcentaje",
main=main, xaxt="n", ylim=range(x))
lines(names(data), x[1,], col="blue")
lines(names(data), x[2,], col="red")
lines(names(data), x[3,], col="yellow")
axis(1, at=names(data))
}
my.plot(effective, main="Votos efectivos")
my.plot(wasted, main="Votos desperdiciados")
my.plot(surplus, main="Votos en exceso")
my.plot(effective/total, main="Votos efectivos")
my.plot(surplus/total, main="Votos en exceso")
my.plot <- function(x, main) {
plot(names(data), x[1,], type="n", xlab="", ylab="Porcentaje",
main=main, xaxt="n", ylim=range(x))
lines(names(data), x[1,], col="royalblue3")
lines(names(data), x[2,], col="firebrick")
lines(names(data), x[3,], col="gold3")
axis(1, at=names(data))
}
my.plot(effective/total, main="Votos efectivos")
my.plot(wasted/total, main="Votos desperdiciados")
my.plot(surplus/total, main="Votos en exceso")
my.plot(effective/total, main="Votos efectivos")
my.plot <- function(x, main) {
plot(names(data), x[1,], type="n", xlab="", ylab="Porcentaje",
main=main, xaxt="n", ylim=range(x))
lines(names(data), x[1,], lwd=3, col="royalblue3")
lines(names(data), x[2,], lwd=3, col="firebrick")
lines(names(data), x[3,], lwd=3, col="gold3")
axis(1, at=names(data))
}
# Plot
my.plot(effective/total, main="Votos efectivos")
my.plot(wasted/total, main="Votos desperdiciados")
my.plot(surplus/total, main="Votos en exceso")
lim <- range(effective/total, wasted/total, surplus/total)
lim
# Function
my.plot <- function(x, main) {
plot(names(data), x[1,], type="n", xlab="", ylab="Porcentaje",
main=main, xaxt="n", ylim=c(0,1))
lines(names(data), x[1,], lwd=3, col="royalblue3")
lines(names(data), x[2,], lwd=3, col="firebrick")
lines(names(data), x[3,], lwd=3, col="gold3")
axis(1, at=names(data))
}
# Plot
my.plot(effective/total, main="Votos efectivos")
my.plot(wasted/total, main="Votos desperdiciados")
my.plot(surplus/total, main="Votos en exceso")
data
# Function
my.plot <- function(x, main) {
plot(names(data), x[1,], type="n", xlab="", ylab="Porcentaje",
main=main, xaxt="n")
lines(names(data), x[1,], lwd=3, col="royalblue3")
lines(names(data), x[2,], lwd=3, col="firebrick")
lines(names(data), x[3,], lwd=3, col="gold3")
axis(1, at=names(data))
}
# Plot
my.plot(effective, main="Votos efectivos")
my.plot <- function(x, main) {
plot(names(data), x[1,], type="n", xlab="", ylab="Porcentaje",
main=main, xaxt="n", ylim=c(0,1))
lines(names(data), x[1,], lwd=3, col="royalblue3")
lines(names(data), x[2,], lwd=3, col="firebrick")
lines(names(data), x[3,], lwd=3, col="gold3")
axis(1, at=names(data))
}
# Plot
my.plot(effective/total, main="Votos efectivos")
my.plot(wasted/total, main="Votos desperdiciados")
my.plot(surplus/total, main="Votos en exceso")
my.plot(surplus/total, main="Votos en exceso")
